﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Helpers;
using AtletIS.Shared.Data;

namespace AtletIS.Shared.Forms
{
    public partial class frmTextInput : Form
    {
        public int status = 0;
        // status:
        // 1 - manualne rozdelenie disciplin
        // 2 - premenovat disciplinu

        public TreeNode tnDiscipline;
        
        public frmTextInput(TreeNode tnDiscipline)
        {
            InitializeComponent();
            this.tnDiscipline = tnDiscipline;
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Form Load
        private void frmTextInput_Load(object sender, EventArgs e)
        {
            if (status == 1)
            {
                lblEnter.Text = "Zadajte názov subdisciplíny pre: " + tnDiscipline.FullPath;
            }

            if (status == 2)
            {
                lblEnter.Text = "Zadajte nový názov pre: " + tnDiscipline.FullPath;
                txtText.Text = tnDiscipline.Text;
            }
        }

        // btnOk Click
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtText.Text.Trim() == string.Empty)
            { 
                MessageBox.Show("Zadajte text, prosím.");
                return;
            }
            
            if (status == 1)
            { 
                DisciplineListTableAdapter taDisciplineList = new DisciplineListTableAdapter();
                aDbDataSet.DisciplineListDataTable dtDisciplineList = null;

                try
                {
                    dtDisciplineList = DatabaseHelper.GetDisciplineListById(Convert.ToInt32(tnDiscipline.Name));
                    if (dtDisciplineList != null && dtDisciplineList.Rows.Count > 0)
                    {
                        DataRow drParent = dtDisciplineList.Rows[0];

                        int secondaryOrder = 1;
                        DataTable dtChildren = DatabaseHelper.GetDisciplineListByParentIdActive(Convert.ToInt32(drParent["ID"]));
                        if (dtChildren != null && dtChildren.Rows.Count > 0)
                        {
                            secondaryOrder = Convert.ToInt32(dtChildren.Rows[dtChildren.Rows.Count - 1]["SecondaryOrder"]) + 1;
                        }

                        AtletIS.Shared.Data.aDbDataSet.DisciplineListRow drDisciplineList = dtDisciplineList.NewDisciplineListRow();

                        drDisciplineList.Name = txtText.Text;
                        drDisciplineList.ParentId = Convert.ToInt32(drParent["ID"]);
                        drDisciplineList.CompetitionId = Convert.ToInt32(drParent["CompetitionId"]);
                        drDisciplineList.DisciplineId = Convert.ToInt32(drParent["DisciplineId"]);
                        drDisciplineList.CategoryId = Convert.ToInt32(drParent["CategoryId"]);
                        drDisciplineList.Status = 1;
                        drDisciplineList.Sync = false;
                        drDisciplineList.MeasurementId = 1;
                        drDisciplineList.Description = string.Empty;
                        drDisciplineList.SetActivePerformanceIdNull();
                        drDisciplineList.SetActivePerformanceHeaderNull();
                        drDisciplineList.ShowSplits = true;
                        drDisciplineList.WebId = 0;
                        drDisciplineList.Active = true;
                        drDisciplineList.Visible = true;
                        drDisciplineList.Modified = DateTime.Now;
                        
                        drDisciplineList.Order = Convert.ToInt32(drParent["Order"]);
                        drDisciplineList.SecondaryOrder = secondaryOrder;
                        drDisciplineList.AutoOrder = DatabaseHelper.GetDisciplineListNewAutoOrder(drDisciplineList.CompetitionId);
                        
                        drDisciplineList.StartTime = Convert.ToDateTime(drParent["StartTime"]);
                        drDisciplineList.SetRealStartTimeNull();
                        drDisciplineList.SetWindNull();
                        drDisciplineList.Params = string.Empty;

                        dtDisciplineList.AddDisciplineListRow(drDisciplineList);
                        taDisciplineList.Update(dtDisciplineList);

                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Vloženie subdisciplíny", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            if (status == 2)
            { 
                DisciplineListTableAdapter taDisciplines = new DisciplineListTableAdapter();
                DataTable dtDisciplines = new DataTable();
                try
                {
                    dtDisciplines = DatabaseHelper.GetDisciplineListById(Convert.ToInt32(tnDiscipline.Name)); // taDisciplines.GetById(Convert.ToInt32(tnDiscipline.Name));
                    if (dtDisciplines.Rows.Count > 0)
                    {
                        dtDisciplines.Rows[0]["Name"] = txtText.Text.Trim();
                        taDisciplines.Update(dtDisciplines.Rows[0]);
                    }
                    
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Premenovanie", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
